package com.xtx.service.impl;

import com.xtx.context.BaseContext;
import com.xtx.dto.OrderDto;
import com.xtx.dto.PayDto;
import com.xtx.entity.Order;
import com.xtx.entity.OrderDetail;
import com.xtx.entity.ShoppingCart;
import com.xtx.mapper.AddressMapper;
import com.xtx.mapper.PayMapper;
import com.xtx.mapper.ShoppingMapper;
import com.xtx.result.Result;
import com.xtx.service.PayService;
import com.xtx.vo.PayOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private PayMapper payMapper;
    @Autowired
    private ShoppingMapper shoppingMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Override
    @Transactional
    public PayOrderVo getPayOrder(OrderDto orderDto) {
        String orderId = UUID.randomUUID().toString();
        addPayOrder(orderDto, orderId);
        deleteShoppingCart();
        PayOrderVo payOrderVo = payMapper.getPayOrder(orderId, BaseContext.getCurrentId());
        return payOrderVo;
    }

    @Override
    public PayOrderVo getPayOrderByOrderId(String id) {
        return payMapper.getPayOrder(id, BaseContext.getCurrentId());
    }

    @Override
    @Transactional
    public Result pay(PayDto payDto) {
        payMapper.pay(payDto,BaseContext.getCurrentId());
        return Result.success();
    }

    @Override
    public void cancelOrder(String id) {
        payMapper.cancelOrder(id,BaseContext.getCurrentId());
    }

    @Transactional
    /**
     * 添加订单
     * @param orderDto
     * @param orderId
     */
    public void addPayOrder(OrderDto orderDto, String orderId) {
        //通过用户id获取购物车列表
        List<ShoppingCart> shoppingCarts = shoppingMapper.getShoppingCartByUserId(BaseContext.getCurrentId());
        //遍历购物车列表放进订单详情表（商品信息）
        shoppingCarts.forEach(shoppingCart -> {
            OrderDetail orderDetail = OrderDetail.builder()
                    .skuId(shoppingCart.getSkuId())
                    .goodsName(shoppingCart.getName())
                    .goodsImage(shoppingCart.getPicture())
                    .price(shoppingCart.getPrice())
                    .count(shoppingCart.getCount())
                    .orderId(orderId)
                    .attrsText(shoppingCart.getAttrsText())
                    .userId(BaseContext.getCurrentId())
                    .build();
            payMapper.addOrderDetail(orderDetail);
        });
        //获取地址信息
        String address = addressMapper.getAddressById(orderDto.getAddressId());
        //添加订单（订单信息）
        Order order = Order.builder()
                .orderId(orderId)
                .userId(BaseContext.getCurrentId())
                .orderStatus(1)
                .totalPrice(orderDto.getAllPrice())
                .payType(orderDto.getPayType())
                .payChannel(orderDto.getPayChannel())
                .buyerMessage(orderDto.getBuyerMessage())
                .deliveryType(orderDto.getDeliveryType())
                .address(address)
                .count(orderDto.getAllCount())
                .orderTime(LocalDateTime.now())
                .orderCompletedTime(LocalDateTime.now().plusMinutes(30)) // 设置订单完成时间为当前时间加上30分钟
                .build();
        payMapper.addOrder(order);
    }
    private void deleteShoppingCart(){
        //删除购物车
        shoppingMapper.deleteShoppingCartByUserId(BaseContext.getCurrentId());
    }
}
